<?php
/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/9/7
 * Time: 23:55
 */

namespace App\Providers;


class XykOrder
{
    protected $options = array(
        "id" => Utils::id,
        "aes_key" => Utils::aes_key,
        "sign_key" => Utils::sign_key,
    );
    //创建订单
    public function createOrder($fee, $tag){
//        $fee = '1';
        $fee = (int)($fee*100);
        $req['body'] = $this->getBody($tag);
        $req['out_trade_no'] = uniqid(mt_rand());
        $req['total_fee'] = (string)($fee);
        $req['return_url'] =Utils::returnUrl;
        $req['notify_url'] =Utils::notifyUrl;
        $pay = new Xyk($this->getOptions($tag));
        $content = $pay->post(Utils::creatrOrderUrl, $req);
        $result = json_decode($content, true);
        $pay->decrypt_result($result);
        return $result;
    }
    //查询订单
    public function selectOrder($orderId, $tag){
        $req['out_trade_no'] = $orderId;
        $pay = new Xyk($this->getOptions($tag));
        $content = $pay->post(Utils::selectOrderUrl, $req);
        $result = json_decode($content, true);
        $pay->decrypt_result($result);
        return $result;
    }
    //取消订单
    public function cancelOrder(){
        $req['out_trade_no'] = $_POST['order_id'];
        $pay = new xyk($this->options);
        $content = $pay->post(Utils::cancleOrderUrl, $req);
        $result = json_decode($content, true);
        $pay->decrypt_result($result);
        echo (json_encode($result));
    }
    //通知
    public function notify(){
        $req['out_trade_no'] =$_GET['order_id'];
        $req['order_id'] = '1514270584761306';
        $req['time_end'] = '2017-12-26 14:43:21';
        $req['uid'] = '2088412828744974';
        $pay = new xyk($this->options);
        $content = $pay->post($this->notifyUrl,$req);
        echo $content;
        // $result = json_decode($content, true);
        // $pay->decrypt_result($result);
        // echo (json_encode($result));
    }
    //回调
    public function testCallBack(){
        $pay = new Xyk($this->options);
        $data['param']=$_POST['param'];
        $data['id']=$_POST['id'];
        $data['sign']=$_POST['sign'];
        $pay->decrypt_data($data);
        $order_id=$data['param']['out_trade_no'];
        $result= header("Location: http://apartment.uncleostrich.cn/notice.json?action=check_notice&order_id={$order_id}");
        echo $result;
    }

    private function getBody($tag) {
        switch ($tag) {
            case 'deposit' : return '押金';
            case 'electric' : return '电费';
            case 'gas' : return '燃气费';
            case 'water' : return '水费';
            case 'rent' : return '租金';
            default : return '押金';
        }
    }

    private function getOptions($tag)
    {
        switch ($tag) {
            case 'deposit' : return ['id' => 2018092504020, 'aes_key' => '3E37EB3F2E63EFD0EB53CF7DA5625768', 'sign_key' => 'Fp3zmAExI$nlgqM%'];
            case 'electric' : return ['id' => 2018092504031, 'aes_key' => '7394124107E1EA2680C1BE971FC2758C', 'sign_key' => 'iim9lAlCKk1gkLdY'];
            case 'gas' : return ['id' => 2018092504050, 'aes_key' => 'FB0B616222BD59E256B37A3D06A6174B', 'sign_key' => 'e36ZB^h4nlbQHFgF'];
            case 'water' : return ['id' => 2018092504014, 'aes_key' => 'ECDE9C38BBD75011DFD326B47AE44236', 'sign_key' => 'Qtv!gfzAfDM6EpGL'];
            case 'rent' : return ['id' => 2018092504054, 'aes_key' => '47E72575F3C19F2E6EC21D6F867797E7', 'sign_key' => 'EDOuE*m&uYkD694Y'];
            default : return ['id' => 2018092504020, 'aes_key' => '3E37EB3F2E63EFD0EB53CF7DA5625768', 'sign_key' => 'Fp3zmAExI$nlgqM%'];
        }
    }
}